<template>
    <div class="box">
        <header class="header">
            <!-- $router.back() -->
            <van-nav-bar title="详情" left-arrow @click-left="$router.go(-1)">
                <template #right>
                    <!-- <van-icon name="search" size="18" /> -->
                    <van-icon name="ellipsis" size="18" @click="showShare = true" />
                </template>
            </van-nav-bar>
        </header>
        <div class="content">
            <!-- {{ detailInfo }} -->
            <div>
                <img :src="detailInfo.image" alt="" style="width:100%;">
            </div>
            <div>{{ detailInfo.title }}</div>
            <div style="color:red"> ￥{{ detailInfo.price }}</div>

        </div>
    </div>
    <footer class="footer">
        <van-action-bar>
            <van-action-bar-icon icon="chat-o" text="客服" color="#ee0a24" />
            <van-action-bar-icon icon="cart-o" text="购物车" @click="$router.push('/cart')" :badge="totalCount" />
            <van-action-bar-icon icon="star" text="已收藏" color="#ff5000" />
            <van-action-bar-button type="warning" text="加入购物车" @click="addCart" />
            <van-action-bar-button type="danger" text="立即购买" />
        </van-action-bar>
    </footer>

    <van-share-sheet v-model:show="showShare" title="立即分享给好友" :options="options" @select="onSelect" />

</template>

<script>
import request from '@/utils/request.js'
import { mapActions, mapState } from 'pinia'
import { useCounterStore } from '@/store/index'
export default {
    data() {
        return {
            detailInfo: {},
            showShare: false,
            options: [
                { name: '微信', icon: 'wechat' },
                { name: '微博', icon: 'weibo' },
                { name: '复制链接', icon: 'link' },
                { name: '分享海报', icon: 'poster' },
                { name: '二维码', icon: 'qrcode' },
            ]
        }
    },
    created() {
        console.log(this.$route.params.id, '-----')
        request.get('/detail', {
            params: {
                id: this.$route.params.id
            }
        }).then(res => {
            if (res.data.code == 200) {
                this.detailInfo = res.data.data
            }
        })
    },
    computed: {
        ...mapState(useCounterStore, ['totalCount']),
    },
    methods: {
        ...mapActions(useCounterStore, ['addItem']),
        addCart() {
            // 判断用户是否登录 -- localStorage是否有token
            if (localStorage.getItem('token')) {
                // 已登录
                // console.log('已登录')
                this.addItem(this.detailInfo)
            } else {
                // 未登录--跳转到登录页面
                this.$router.push('/login')
            }
        }
    }
}

</script>